﻿
package {
	import flash.display.MovieClip;
	import flash.display.Sprite;
	public class Ground extends MovieClip{
		var cameraX:Number=0;
		var cameraY:Number=0;
		var couldLayer:Sprite=new Sprite();
		var mountainLayer:Sprite=new Sprite();
		var backLayer:Sprite=new Sprite();
		var roadLayer:Sprite=new Sprite();
		var rabbitLayer:Sprite=new Sprite();
		var foreLayer:Sprite=new Sprite();
		var asterLayer:Sprite=new Sprite();
		
										 
		var could:Array=new Array(); 
		var mountain:Array=new Array();
		var back:Array=new Array();
		var road:Array=new Array();
		var fore:Array=new Array();
		var aster:Array=new Array();
		var roadLength:int=0; //地面长度
		var roadStart:int=0;   //下一段地面起始位置
		var wallStart:int=0;
		var rabbit:MovieClip;
		const screenWidth=550;
		const mountainWidth=128;
		const couldWidth=512;
		public function Ground(could:Sprite,mountain:Sprite,back:Sprite,road:Sprite,rabbit:Sprite,fore:Sprite,aster:Sprite):void{
			couldLayer=could;
			mountainLayer=mountain;
			backLayer=back;
			roadLayer=road;
			rabbitLayer=rabbit;
			foreLayer=fore;
			asterLayer=aster;
		}
		public function Init(r:MovieClip):void{
			cameraX=0;
			remove();
			
			var tempCould:云1=new 云1();
			tempCould.x=0;
			couldLayer.addChild(tempCould);
			could.push(new Array(tempCould,tempCould.width));
			
			var tempMountain:远景=new 远景();
			tempMountain.x=0;
			mountainLayer.addChild(tempMountain);
			mountain.push(new Array(tempMountain,tempMountain.width));
			
			roadStart=0;
			roadLength=12;
			addRoad();
			rabbit=r;
			rabbit.x=100;
			rabbit.y=-1;
			moveCamera(0,0);
		}
		public function rabbitBump():int{
			//rabbit.y+=1;
			var radish:int=0;
			for (var i:String in road){
				if (rabbit.hitTestObject(road[i][0])){
					if (rabbit.oldY<road[i][0].y){
						rabbit.y=road[i][0].y-0.7;
						rabbit.floor();
					}else{
						rabbit.x=road[i][0].x-16;
					}
				}
			}
			for (i in back){
				if (rabbit.hitTestObject(back[i][0])){
					if (back[i][2]==1 && rabbit.oldY<back[i][0].y){
						rabbit.y=back[i][0].y-1;
						rabbit.floor();
					}else if(back[i][2]==10){//萝卜
						var tempAster1:星星=new 星星();
						tempAster1.x=back[i][0].x+Math.random()*20-10;
						tempAster1.y=back[i][0].y+Math.random()*20-10;
						asterLayer.addChild(tempAster1);
						aster.push(tempAster1);
						var tempAster2:星星=new 星星();
						tempAster2.x=back[i][0].x+Math.random()*20-10;
						tempAster2.y=back[i][0].y+Math.random()*20-10;
						asterLayer.addChild(tempAster2);
						aster.push(tempAster2);
						var tempAster3:星星=new 星星();
						tempAster3.x=back[i][0].x+Math.random()*20-10;
						tempAster3.y=back[i][0].y+Math.random()*20-10;
						asterLayer.addChild(tempAster3);
						aster.push(tempAster3);
						backLayer.removeChild(back[i][0]);
						back.splice(i,1);
						radish++;
					}
				}
			}
			for(i in aster){
				aster[i].alpha-=0.05;
				if (aster[i].alpha<=0){
					asterLayer.removeChild(aster[i]);
					aster.splice(i,1);
				}
			}
			return radish;
		}
		public function moveCamera(x:Number,y:Number):void{
			cameraX+=x;
			cameraY+=y;
			couldLayer.x=cameraX*-0.1;
			couldLayer.y=42-cameraY*0.1;
			mountainLayer.x=cameraX*-0.5;
			mountainLayer.y=170-cameraY*0.5;
			backLayer.x=-cameraX;
			backLayer.y=275-cameraY;
			roadLayer.x=-cameraX;
			roadLayer.y=275-cameraY;
			rabbitLayer.x=-cameraX;
			rabbitLayer.y=275-cameraY;
			foreLayer.x=-cameraX;
			foreLayer.y=275-cameraY;
			asterLayer.x=-cameraX;
			asterLayer.y=275-cameraY;
			if (could[0][1]+couldLayer.x<0){
				couldLayer.removeChild(could[0][0]);
				could.shift();
			}
			if (mountain[0][1]+mountainLayer.x<0){
				mountainLayer.removeChild(mountain[0][0]);
				mountain.shift();
			}
			if (road[0][2]+roadLayer.x<0){
					roadLayer.removeChild(road[0][0]);
					roadLayer.removeChild(road[0][1]);
					road.shift();
			}
			for (var i:String  in back){
				if (back[i][1]+backLayer.x<0){
					backLayer.removeChild(back[i][0]);
					back.splice(i,1);
				}
			}
			for (i in fore){
				if (fore[i][1]+foreLayer.x<0){
					foreLayer.removeChild(fore[i][0]);
					fore.splice(i,1);
				}
			}
			
			addBackGround();
			
			if (roadStart+roadLayer.x<screenWidth){
				var pitLength:int=Math.random()*100+50;
				addPit(roadStart-8,roadStart+pitLength);
				roadStart+=pitLength;
				roadLength=Math.random()*14+1;
				var wallLength:int=0;
				if (roadLength>4 && Math.random()*10<1){
					wallLength=Math.random()*(roadLength-6)+3;
					wallStart=roadStart+Math.random()*(roadLength-wallLength)*64;
					addRadish(wallStart,wallLength,3);
					addWall(3,wallLength);
					
					wallLength=Math.random()*(roadLength-4)+2;
					wallStart=roadStart+Math.random()*(roadLength-wallLength)*64;
					addRadish(wallStart,wallLength,2);
					addWall(2,wallLength);
					
					wallLength=Math.random()*(roadLength-4)+2;
					wallStart=roadStart+Math.random()*(roadLength-wallLength)*64;
					addRadish(wallStart,wallLength,1);
					addWall(1,wallLength);
				}else if(roadLength>3 && Math.random()*5<1){
					wallLength=Math.random()*(roadLength-4)+2;
					wallStart=roadStart+Math.random()*(roadLength-wallLength)*64;
					addRadish(wallStart,wallLength,2);
					addWall(2,wallLength);
					wallLength=Math.random()*(roadLength-3)+2;
					wallStart=roadStart+Math.random()*(roadLength-wallLength)*64;
					addRadish(wallStart,wallLength,1);
					addWall(1,wallLength);
				}else if (roadLength>2 && Math.random()*5<1){
					wallLength=Math.random()*(roadLength-2)+2;
					wallStart=roadStart+Math.random()*64;
					addRadish(wallStart,wallLength,1);
					addWall(1,wallLength);
				}
				addRadish(roadStart,roadLength,0);
				addRoad();
			}
		}
		
		
		//添加场景
		private function addRadish(radishStart:Number,radishLength:Number,layer:int):void{
			var s:Number=Math.floor(Math.random()*64);
			var l:int=Math.random()*(4-layer);
			if (l>radishLength){
				return void;
			}
			s+=Math.floor(Math.random()*l)*64;
			for (var i:Number=1;i<radishLength-l;i++){
				var radish:萝卜=new 萝卜();
				radish.x=radishStart+s+i*64;
				radish.y=-layer*64-20;
				backLayer.addChild(radish);
				back.push(new Array(radish,radish.x,10));
			}
		}
		private function addRoad():void{
			addRoadL();
			for (var i:int=0;i<roadLength;i++){
				var tempRoad1:土地=new 土地();
				tempRoad1.x=roadStart;
				tempRoad1.y=0;
				roadLayer.addChild(tempRoad1);
				var tempRoad2:地面=new 地面();
				tempRoad2.x=roadStart;
				tempRoad2.y=-1;
				tempRoad2.gotoAndStop(Math.random()*8<7?1:2);
				roadLayer.addChild(tempRoad2);
				roadStart+=tempRoad1.width;
				road.push(new Array(tempRoad1,tempRoad2,roadStart));
				
				if (Math.random()*10<1){
					var tempRoad3:树远=new 树远();
					tempRoad3.x=roadStart-tempRoad3.width/2-tempRoad1.width+Math.random()*tempRoad1.width;
					tempRoad3.y=-tempRoad3.height;
					backLayer.addChild(tempRoad3);
					back.push(new Array(tempRoad3,tempRoad3.x+tempRoad3.width));
				}
				if (Math.random()*15<1){
					var tempRoad4:树近=new 树近();
					tempRoad4.x=roadStart-tempRoad4.width/2-tempRoad1.width+Math.random()*tempRoad1.width;
					tempRoad4.y=-tempRoad4.height;
					foreLayer.addChild(tempRoad4);
					fore.push(new Array(tempRoad4,tempRoad4.x+tempRoad4.width));
				}
			}
			addRoadR();
		}
		private function addRoadL():void{
			var tempRoad1:土地左=new 土地左();
			tempRoad1.x=roadStart;
			tempRoad1.y=0;
			roadLayer.addChild(tempRoad1);
			var tempRoad2:地面左=new 地面左();
			tempRoad2.x=roadStart;
			tempRoad2.y=-1;
			roadLayer.addChild(tempRoad2);
			
			roadStart+=tempRoad1.width;
			road.push(new Array(tempRoad1,tempRoad2,roadStart));
		}
		private function addRoadR():void{
			var tempRoad1:土地右=new 土地右();
			tempRoad1.x=roadStart;
			tempRoad1.y=0;
			roadLayer.addChild(tempRoad1);
			var tempRoad2:地面右=new 地面右();
			tempRoad2.x=roadStart;
			tempRoad2.y=-1;
			roadLayer.addChild(tempRoad2);
			
			roadStart+=tempRoad1.width;
			road.push(new Array(tempRoad1,tempRoad2,roadStart));
		}
		private function addPit(pitX:int,pitLength:int):void{
			while (pitX<=pitLength){
				var tempPit:坑壁=new 坑壁();
				tempPit.x=pitX;
				tempPit.y=0;
				backLayer.addChild(tempPit);
				pitX+=tempPit.width;
				back.push(new Array(tempPit,pitX));
			}
		}
		private function addBackGround():void{ 
			while (could[could.length-1][1]+couldLayer.x<screenWidth){
				var tempCould:云1=new 云1();
				tempCould.x=could[could.length-1][1];
				couldLayer.addChild(tempCould);
				could.push(new Array(tempCould,tempCould.x+couldWidth));
			}
			while (mountain[mountain.length-1][1]+mountainLayer.x<screenWidth){
				var tempMountain:远景=new 远景();
				tempMountain.x=mountain[mountain.length-1][1];
				tempMountain.gotoAndPlay(mountain[mountain.length-1][0].currentFrame);
				mountainLayer.addChild(tempMountain);
				mountain.push(new Array(tempMountain,tempMountain.x+mountainWidth));
			}
		}
		private function addWall(layer:int,wallLength:int):void{
			addWallL(layer);
			addWallM(layer,wallLength);
			addWallR(layer);
		}
		private function addWallL(layer:int):void{
			for (var j:int=1;j<=layer*2;j++){
				var tempWall:石墙左=new 石墙左();
				tempWall.x=wallStart;
				tempWall.y=-tempWall.height*(layer*2-j+1);
				backLayer.addChild(tempWall);
				back.push(new Array(tempWall,tempWall.x+tempWall.width,j));
			}
			var tempWall2:地面左=new 地面左();
			tempWall2.x=wallStart;
			tempWall2.y=-back[back.length-1][0].height*layer*2-1;
			backLayer.addChild(tempWall2);
			back.push(new Array(tempWall2,tempWall2.x+tempWall2.width,0));
			wallStart+=8;
		}
		private function addWallM(layer:int,wallLength:int):void{
			for (var i:int=0;i<wallLength;i++){
				for (var j:int=1;j<=layer*2;j++){
					var tempWall:石墙=new 石墙();
					tempWall.x=wallStart;
					tempWall.y=-tempWall.height*(layer*2-j+1);
					backLayer.addChild(tempWall);
					tempWall.gotoAndStop(Math.random()*10<1?2:Math.random()*15<1?3:1);
					back.push(new Array(tempWall,wallStart+tempWall.width,j));
				}
				var tempWall2:地面=new 地面();
				tempWall2.x=wallStart;
				tempWall2.y=-back[back.length-1][0].height*layer*2-1;
				tempWall2.gotoAndStop(Math.random()*6<5?1:2);
				backLayer.addChild(tempWall2);
				back.push(new Array(tempWall2,tempWall2.x+tempWall2.width,0));
				wallStart+=64;
			}
		}
		private function addWallR(layer:int):void{
			for (var j:int=1;j<=layer*2;j++){
				var tempWall:石墙右=new 石墙右();
				tempWall.x=wallStart;
				tempWall.y=-tempWall.height*(layer*2-j+1);
				backLayer.addChild(tempWall);
				back.push(new Array(tempWall,wallStart+tempWall.width,j));
			}
			var tempWall2:地面右=new 地面右();
			tempWall2.x=wallStart;
			tempWall2.y=-back[back.length-1][0].height*layer*2-1;
			backLayer.addChild(tempWall2);
			back.push(new Array(tempWall2,tempWall2.x+tempWall2.width,0));
			wallStart+=8;
		}
		public function remove():void{
			couldLayer.x=0;
			mountainLayer.x=0;
			backLayer.x=0;
			roadLayer.x=0;
			rabbitLayer.x=0;
			foreLayer.x=0;
			
			for (var i:String in could){
				couldLayer.removeChild(could[i][0]);
			}
			could.length=0;
			for (i in mountain){
				mountainLayer.removeChild(mountain[i][0]);
			}
			mountain.length=0;
			for (i in back){
				backLayer.removeChild(back[i][0]);
			}
			back.length=0;
			for (i in road){
				roadLayer.removeChild(road[i][0]);
				roadLayer.removeChild(road[i][1]);
			}
			road.length=0;
			for (i in fore){
				foreLayer.removeChild(fore[i][0]);
			}
			fore.length=0;
		}
	}
}
			
			
			